#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

struct listNode
{
	char *data;
	struct listNode *nextPtr;
};

typedef struct listNode ListNode;
typedef ListNode *ListNodePtr;

struct list
{
	ListNodePtr head;
	ListNodePtr tail;
};

typedef struct list List;
typedef List *ListPtr;

void insert(ListPtr *head, ListPtr *tail, char *value);
char delete(ListPtr *head, ListPtr *tail);
int isEmpty(ListPtr head);
void printList(ListPtr currPtr);
void instructions(void);

int main(void)
{
	ListPtr head = NULL;
	ListPtr tail = NULL;

	int choice;
	char item[80];


	instructions();
	printf("?");
	scanf("%d", &choice);

	while(choice !=3)
	{
		switch(choice)
		{
			case 1:

			printf("Enter a character string: \n");
			scanf("%s", item);

			insert(head, tail, item);
			printList(head);

			break;

			case 2:
			if(!isEmpty(head))
			{
				item = delete(head, tail);
				printf("%s removed.\n", item);
			}
			printList(head);
			break;

			default:
			printf("Invalid choice.\n");
			instructions();
		}

		printf("?");
		scanf("%d", &choice);
	}

	printf("End.\n");
	return 0;

}

void instructions(void)
{
	printf("Enter your choice:\n"
	    "   1 to insert an element into the list.\n"
	    "   2 to delete an element from the list.\n"
	    "   3 to end.\n");
}

void insert(ListPtr *head, ListPtr *tail, char *value)
{
	ListNodePtr newPtr;

	newPtr = malloc(sizeof(ListNode));
	newPtr->nextPtr=NULL;
	newPtr->data = malloc(strlen(value)+1 * sizeof(char));

	strcpy(newPtr->data, value);

	if(newPtr != NULL)
	{
		newPtr -> data = value;
		newPtr -> nextPtr = NULL;

		if(!isEmpty(*head))
		{
			*head = newPtr;
		}
		else
		{
			(*tail) -> nextPtr = newPtr;
		}

		*tail = newPtr;
	}
	else
	{
		printf("%s not inserted.  No memory avaliable.\n", value);
	}

}

char delete(ListPtr *head, ListPtr *tail)
{
	char value;
	ListNodePtr tempPtr;
	
	value = (*head) -> data;
	tempPtr = *head;
	*head = (*head) -> nextPtr;


	if(*head == NULL)
	{
		*tail = NULL;
	}
	free(tempPtr);
	return value;
}

int isEmpty(ListPtr head)
{
	return head == NULL;
}

void printList(ListPtr currPtr)
{
	if(currPtr->head == NULL)
	{
		printf("List is empty.\n");
	}
	else
	{
		printf("The list is: \n");
		while(currPtr != NULL)
		{
			printf("%s  ", currPtr -> data);
			currPtr = currPtr->nextPtr;
		}
		printf("NULL\n");
	}
}